<template>
  <div class="avatar-management-overlay" @click.self="handleClose">
    <el-card class="card">
      <div class="avatar-section">
        <h3>当前头像</h3>
        <img :src="avatar" alt="用户头像" class="avatar" />
      </div>
      <div class="upload-section">
        <el-form label-position="top">
          <el-form-item label="上传新头像">
            <el-upload
                class="upload-demo"
                drag
                :action="uploadUrl"
                :show-file-list="false"
                :on-success="handleUploadSuccess"
                :before-upload="beforeUpload"
                :on-error="handleUploadError"
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <el-button class="close-button" type="text" @click="handleClose">
        <el-icon><Close /></el-icon>
      </el-button>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { Close } from '@element-plus/icons-vue';

const avatar = ref('https://via.placeholder.com/120');
const uploadUrl = ref('http://localhost:9090/upload');

const emit = defineEmits(['close']);

const handleUploadSuccess = (response) => {
  if (response.code === 200) {
    avatar.value = response.data.url;
    ElMessage.success('上传成功！');
  } else {
    ElMessage.error('上传失败：' + response.message);
  }
};

const handleUploadError = (err) => {
  console.error('上传错误：', err);
  ElMessage.error('上传错误，请重试！');
};

const beforeUpload = (file) => {
  const isImage = file.type.startsWith('image/');
  if (!isImage) {
    ElMessage.error('只能上传图片文件！');
  }
  return isImage;
};

const handleClose = () => {
  emit('close');
};
</script>

<style scoped>
.avatar-management-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw; /* 覆盖整个视口宽度 */
  height: 100vh; /* 覆盖整个视口高度 */
  background: rgba(0, 0, 0, 0.5); /* 半透明遮罩 */
  display: flex;
  justify-content: flex-end; /* 将组件放置在右侧 */
  align-items: flex-start; /* 将组件放置在顶部 */
  z-index: 1000; /* 确保组件显示在最上层 */
}

.card {
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  background: var(--card-bg);
  width: 320px; /* 固定宽度 */
  position: relative;
  animation: slide-in 0.3s ease; /* 添加动画效果 */
  margin: 20px; /* 与页面边缘保持间距 */
}

@keyframes slide-in {
  from {
    transform: translateX(100%);
  }
  to {
    transform: translateX(0);
  }
}

.avatar-section {
  text-align: center;
  margin-bottom: 20px;
}

.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #ddd;
}

.upload-section {
  max-width: 400px;
  margin: 0 auto;
}

.el-upload-dragger {
  border: 2px dashed var(--primary-color);
  background: rgba(64, 158, 255, 0.05);
  transition: all 0.3s ease;
}

.el-upload-dragger:hover {
  border-color: var(--primary-color);
  background: rgba(64, 158, 255, 0.1);
}

.close-button {
  position: absolute;
  top: 10px;
  right: 10px;
  color: var(--text-secondary);
  transition: color 0.3s ease;
}

.close-button:hover {
  color: var(--primary-color);
}
</style>